package com.yyfax.ph.personal.mymoney.cash;

import android.text.TextUtils;
import com.yyfax.infrastructure.eventbus.EventAgent;
import com.yyfax.infrastructure.mvp.IView;
import com.yyfax.infrastructure.mvp.Presenter;
import com.yyfax.infrastructure.mvp.PresenterFactory;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

/**
 * Created by xiongyufeng on 2017/11/11.
 * Copyright  2017 www.yylending.com. All Rights Reserved.
 */
public class DrawDetailPresenter extends Presenter<DrawDetailModel, DrawDetailPresenter.View> {
	private DrawDetailPresenter.View mView;

	public static class Generator implements PresenterFactory<DrawDetailPresenter> {
		@Override
		public DrawDetailPresenter create() {
			return new DrawDetailPresenter();
		}
	}

	public interface View extends IView {
		void refreshWithDrawDetail(int cnt, int amountSum, List<DrawDetailModel.DrawInfo> list);

		void loadMoreWithDrawDetail(int cnt, int amountSum, List<DrawDetailModel.DrawInfo> list);

		void getWithDrawFailure(String tips);
	}

	@Override
	public void attach(DrawDetailPresenter.View view) {
		mView = view;
		mModel = new DrawDetailModel();
		EventAgent.register(this);
	}

	@Override
	public void detach() {
		mView = null;
		mModel = null;
		EventAgent.unregister(this);
	}

	public void getWithDrawList(int pageNo, int pageSize, String startDate) {
		DrawDetailModel.WithDrawParams detail = new DrawDetailModel.WithDrawParams();
		detail.pageSize = pageSize;
		detail.pageNo = pageNo;
		detail.startDate = startDate;
		if (pageNo > 1) {
			execute(detail, DrawDetailModel.MSG_LOAD_MORE_WITHDRAW_DETAILS, false);
		} else {
			execute(detail, DrawDetailModel.MSG_REFRESH_WITHDRAW_DETAILS, false);
		}
	}

	@Subscribe(threadMode = ThreadMode.MAIN)
	public void getWithDrawResult(DrawDetailModel.WithDrawDetailResult result) {
		if (isResponseSuccess(result)) {
			if (result.requestCode == RebateDetailModel.MSG_REFRESH_REBATE_DETAILS) {
				mView.refreshWithDrawDetail(result.getContent().cnt, result.getContent().amountSum, result.getContent().accountDetailForDrawList);
			} else {
				mView.loadMoreWithDrawDetail(result.getContent().cnt, result.getContent().amountSum, result.getContent().accountDetailForDrawList);
			}
			return;
		}
		mView.getWithDrawFailure(TextUtils.isEmpty(result.getMsg()) ? "获取数据失败!" : result.getMsg());
	}
}
